package com.roper.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roper.yygh.common.result.Result;
import com.roper.yygh.common.utils.MD5;
import com.roper.yygh.hosp.service.HospitalSetService;
import com.roper.yygh.model.hosp.Hospital;
import com.roper.yygh.model.hosp.HospitalSet;
import com.roper.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

/**
 * Description: 医院设置 Controller
 * Date:      2021/6/21 20:55
 * author:    LPH
 * version:   V1.0
 */
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {

    @Autowired
    HospitalSetService hospitalSetService;

    // 查询医院设置表里的所有数据
    @ApiOperation(value = "获取所有医院设置列表")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        // 调用service 里的方法
        List<HospitalSet> list = hospitalSetService.list();

        return Result.ok(list);
    }

    // 根据id删除医院设置
    @ApiOperation(value = "根据id删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if(flag) {
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    // 3, 条件查询带分页
    @PostMapping("/findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        // 1. 设置Page对象 传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);

        // 2. 构建查询条件
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVo.getHoscode();// 医院设置编号
        String hosname = hospitalSetQueryVo.getHosname();// 医院设置名称
        if(!StringUtils.isEmpty(hoscode)) {
            queryWrapper.eq("hoscode", hoscode);
        }
        if(!StringUtils.isEmpty(hosname)){
            queryWrapper.like("hosname", hosname);
        }
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    // 4, 添加医院设置
    @PostMapping("/saveHospSet")
    public Result saveHospSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态，0 不能使用 1 可以使用
        hospitalSet.setStatus(1);
        Random random = new Random();
        // 密钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+ random.nextInt(1000)));

        // 调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) {
            return Result.ok();
        }else{
            return Result.fail();
        }
    }
    // 5, 根据id获取医院设置
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    // 6, 修改医院设置
    @PostMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet){
        boolean update = hospitalSetService.updateById(hospitalSet);
        if(update) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    // 7, 批量删除医院设置
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHospSet(@RequestBody List<Long> list) {
        hospitalSetService.removeByIds(list);
        return Result.ok();
    }

    // 8, 医院设置锁定和解锁功能
    @PutMapping("/lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id,
                              @PathVariable Integer status) {

        // 根据id查询出医院的设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 传入锁定和解锁
        hospitalSet.setStatus(status);
        // 调用方法 返回结果
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    // 9, 发送签名密钥
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey(); // 密钥
        String hoscode = hospitalSet.getHoscode(); // 医院编号
        // TODO 发送短信
        return Result.ok();
    }
}
